package freshmanspecial.mredrock.com.welcomefreshman.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import freshmanspecial.mredrock.com.welcomefreshman.Adapter.QQSearchRecyclerAdapter;
import freshmanspecial.mredrock.com.welcomefreshman.Base.BaseFragment;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.QQGroupBean;
import freshmanspecial.mredrock.com.welcomefreshman.Model.StrategyModel;
import freshmanspecial.mredrock.com.welcomefreshman.R;

/**
 * Created by HP on 2017/8/13.
 */

public class Stragegy_qqsearch_fragment extends BaseFragment {
    private EditText editText;
    private ImageView delete;
    private RecyclerView recyclerView;
    private TextView backOrSearch;
    private boolean isEmpty = true,isFound=false;
    private QQGroupBean qqGroupBean;
    private ArrayList<String> beabList;

    @Override
    protected void addDefaultData() {

    }

    @Override
    protected void initView() {
        beabList = new ArrayList<>();
        editText = $(R.id.edit_search_qq);
        delete = $(R.id.edit_clear_qq);
        backOrSearch = $(R.id.cancel_search);
        recyclerView  = $(R.id.list_qq_result);
        getData();
        setClear();
        setSearch();

    }

    private void setClear() {
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("");
            }
        });
    }
    private void setList() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(new QQSearchRecyclerAdapter(beabList,getContext()));
    }

    private void setSearch() {
        editText.setOnKeyListener(new View.OnKeyListener() {//输入完后按键盘上的搜索键【回车键改为了搜索键】

            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER) {//修改回车键功能
                    ((InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getActivity().getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    String obj = editText.getText().toString();
                    if (obj.equals("")) {
                        Toast.makeText(getActivity(),"请输入要搜索的内容",Toast.LENGTH_SHORT).show();
                    } else {
                        beabList.clear();
                        for(int i=0;i<qqGroupBean.getData().getCollege().size();i++)
                        {
                            if(qqGroupBean.getData().getCollege().get(i).getGroupName().contains(obj) )
                            {
                                beabList.add(qqGroupBean.getData().getCollege().get(i).getGroupName()+qqGroupBean.getData().getCollege().get(i).getNumber());
                            }
                            if(qqGroupBean.getData().getHomeland().get(i).getGroupName().contains(obj))
                            {
                                beabList.add(qqGroupBean.getData().getHomeland().get(i).getGroupName()+qqGroupBean.getData().getHomeland().get(i).getNumber());

                            }
                        }
                        setList();
                    }
                    return true;
                }
                return false;
            }
        });
    }



    private void getData() {
        StrategyModel strategyModel = new StrategyModel();
        strategyModel.getQQgroup(new StrategyModel.OnGetData() {
            @Override
            public void onFinish(Object dataBean) {
                qqGroupBean = (QQGroupBean) dataBean;
            }
        });
    }

    @Override
    protected int getResourceId() {
        return R.layout.special_2017_strategt_qqsearch_fragment;
    }
}
